package com.fare.taluo.application.impl.plane.agent;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.BaseEntity;
import com.fare.taluo.application.plane.agent.AgentApplication;
import com.fare.taluo.application.plane.flight.AirlineApplication;
import com.fare.taluo.core.domain.config.channel.AgentEntity;
import com.fare.taluo.core.domain.config.channel.SourceEntity;
import com.fare.taluo.core.domain.config.line.FreightPublishEntity;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.core.AgentEntityVO;

@Transactional
@Scope("singleton")
@Service("agentApplication")
public class AgentApplicationImpl implements AgentApplication{

	@Override
	public AgentEntityVO findAgentEntityByAgentCode(String agentCode) {
		AgentEntity entity = new AgentEntity();
		List<AgentEntity> list =  entity.findAgentEntityByAgentCode(agentCode);
		List<AgentEntityVO> voList = this.entity2Vo(list);
		return StringUtils.checkList(voList)?voList.get(0):null;
		
	}
	
	
	
	private List<AgentEntityVO> entity2Vo(List<AgentEntity> list){
		List<AgentEntityVO> voList = new ArrayList<AgentEntityVO>();
		if(StringUtils.checkList(list)){
			for (AgentEntity agentEntity : list) {
				AgentEntityVO vo = new AgentEntityVO();
				vo.setId(agentEntity.getId());
				vo.setAgentCode(agentEntity.getAgentCode());
				vo.setName(agentEntity.getName());
				vo.setCname(agentEntity.getCname());
				vo.setAddress(agentEntity.getAddress());
				vo.setOriginalPrice(agentEntity.getOriginalPrice());
				vo.setPhone(agentEntity.getPhone());
				vo.setPrice(agentEntity.getPrice());
				vo.setType(agentEntity.getType());
				vo.setWeb(agentEntity.getWeb());
				vo.setChannel(agentEntity.getChannel());
				vo.setChannelName(agentEntity.getChannelName());
				voList.add(vo);
			}
		}
		return voList;
	}
	@Override
	public List<AgentEntityVO> findAll() {
		AgentEntity entity = new AgentEntity();
		 List<AgentEntityVO> voList = entity2Vo(entity.findAll());
		return voList;
	}
	@Override
	public void saveOrUpdate(AgentEntityVO vo) {
		AgentEntity entity = new AgentEntity();
		SourceEntity source = new SourceEntity();
		
		entity.setId(vo.getId());
		entity.setAgentCode(vo.getAgentCode());
		entity.setName(vo.getName());
		entity.setCname(vo.getCname());
		entity.setAddress(vo.getAddress());
		entity.setOriginalPrice(vo.getOriginalPrice());
		entity.setPhone(vo.getPhone());
		entity.setPrice(vo.getPrice());
		entity.setType(vo.getType());
		entity.setWeb(vo.getWeb());
		entity.setChannel(vo.getChannel());
		entity.setChannelName(source.findByHbaseKey(vo.getChannel()).getName());
		entity.saveOrUpdate(entity);
	}
	public boolean entityExist(String agentCode,String channel){
		AgentEntity entity = new AgentEntity();
		return entity.entityExist( agentCode, channel);
	}



	@Override
	public void remove(long dataId) {
		AgentEntity fpe = BaseEntity.get(AgentEntity.class, dataId);
		fpe.remove();
		
	}
}
